﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Management;



namespace regExport
{
   
    class Program
    {
        //Basekey and basefilepath are global in order to save time in hard coding the values. 
      
        static string basekey = "HKEY_LOCAL_MACHINE\\Software\\RCI";
        static string basefilepath = "C:\\myLocal.reg";
        //Made arguement number a global variable in order to test the for loops for the program. 
        static int argnumber = 4;

        static void Main(string[] args)
        {
          
                //Running a if statement if theres less than 4 arugments being passed. 
                //The program will print out a message if there is less than 4 arguments being passed. 
            if (args.Length < argnumber)
            {
                //This help method will tell the user that the number of agruments is wrong. 
                Helper.showHelp();            
            }

            else
            {
                
                //For loop to capture all the agruments that being passed into the program. 
                Console.WriteLine("SourceServer = [{0}]",args[0]);
                Console.WriteLine("DestinatnioServer = [{0}]", args[1]);
           
                string SourceServerName = args[0];
                string DestinatinoServerName = args[1]; 
                string FinalFilePath = "\\\\" + SourceServerName + "\\" + basefilepath.Replace(":", "$");
                string DestFinalPath = "\\\\" +DestinatinoServerName + "\\" + basefilepath.Replace(":", "$");
               
               //Exporting regisrty from the source server.
               //The SourceSourceName is the first argurment that is being passed.
               wmi.ExportReg(basekey, basefilepath, SourceServerName);

                //The replacement method is replacing the value that is needeed to be change. 
                //The copy method is copy the file i just imported from the Source Server. 
                //I am using a for the args array to get all the arguments that are being passed. 

               for (int i = 2; i < args.Length - 1; i += 2)
                {
               Copying_Replacing.Replacement(FinalFilePath, args[i], args[i + 1]);
                }

               Copying_Replacing.Copy(FinalFilePath, DestFinalPath);
                

                //This method will backup registry before importing to destination server. 

                string timestamp = DateTime.Now.ToString();
                timestamp = timestamp.Replace("/", "_");
                timestamp = timestamp.Replace(" ", "_");
                timestamp = timestamp.Replace(":", "_");
                timestamp =  "C:\\backup_" + timestamp.Replace(":", "_") + ".reg";
                //This is the back for a copy of the orginal in case of anything change. 
                wmi.ExportReg(basekey, timestamp, DestinatinoServerName);
              
                //Exporting the registry file with the replacement to the Destination Server.
                //The DestinationSourceName is the last argurment that is being passed.
                wmi.ImportReg(basekey, basefilepath, DestinatinoServerName);

            }

                 //Tells the user that the proccess has finish. It display "Completed". 
            Helper.Finsih();
        }
   
      
    }

    }

